package mest.controller.Modeling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import mest.entity.apart_file;
import mest.entity.car_file;
import mest.entity.customer_file;
import mest.entity.save_file;
import mest.service.Modeling.IapartFileService;
import mest.util.ResultObj;
import mest.util.mestUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-15 21:16
 */
//Model是属于请求域
//           @RequestMapping(value="/one",params = "method=a")
//           @RequestMapping(value="/two1",produces = {"application/json;charset=utf-8","text/plain;charset=utf-8"})
//参数:@RequestParam(value = "b",required = false,defaultValue = "握草尼玛")
//         @RequestBody String(str) 输出:userCode=admin&userPassword=1234567
//          @SessionAttributes({"a","b","c"}) 自动把该controller下所有返回页面的属性的作用域改成session
//          @ModelAttribute标注一个方法时，指定每次跳入某个路径时该方法先执行
@RestController
@CrossOrigin
@RequestMapping(value = "/apartFile")
public class apartFile {


    @Resource
    private IapartFileService iafs;

    @RequestMapping(value = "/insert")
    public String insert(@RequestBody JSONObject json, HttpServletRequest request) {
            System.out.println("我进来了");
            apart_file record = json.toJavaObject(apart_file.class);
            String theirDb = mestUtil.getCompanyDB(request);
            System.out.println(record.toString());
            return iafs.insertapartFile(record, theirDb);
    }

    @RequestMapping(value = "/baseSelect", produces = {"application/json;charset=utf-8", "text/plain;charset=utf-8"})
    public String baseSelect(int index, int pageSize, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        List<apart_file> apartFilePage = iafs.findApartFilePage(index, pageSize, theirDb);
        int i = iafs.selectCount(theirDb);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", i);
        map.put("data", apartFilePage);
        return JSON.toJSONString(map);
    }


    @RequestMapping(value = "/serialize")
    public String serialize(String sf, HttpServletRequest request) {
        String theirDb = mestUtil.getCompanyDB(request);
        System.out.println(sf);
//        JSONArray sf1=JSON.parseArray(sf);

        List<save_file> list = JSONArray.parseArray(sf, save_file.class);

        for (save_file sf1 : list) {
            System.out.println(sf1);
        }
        return "成功";
    }


    /**
     * 删除方法
     */
    @RequestMapping("/delModel")
    public Object delModel(@RequestBody JSONObject json, HttpServletRequest request) {
        Map<String,Object> map = json.toJavaObject(Map.class);
        Integer id = (Integer)map.get("id");
        System.out.println("这里的ID是："+id);
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if (iafs.delModel(id,theirDb) > 0) {
            modelMap.put("erg", "删除成功");
            return JSON.toJSONString(modelMap);
        } else {
            modelMap.put("erg", "不成功");
            return JSON.toJSONString(modelMap);
        }
    }


    @RequestMapping("/queryAll")
    public Object queryAll(Integer index, Integer pageSize, String htmlModel, HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String theirDb = mestUtil.getCompanyDB(request);

        try {
            List<apart_file> apartFile = iafs.queryAll(index, pageSize, htmlModel, theirDb);
            if (!ObjectUtils.isEmpty(apartFile)) {
                modelMap.put("data", apartFile);
                modelMap.put("msg", "success");
                modelMap.put("code", 0);
                modelMap.put("count", iafs.findCount(htmlModel, theirDb));
            } else {
                modelMap.put("msg", "此对象为空值");
            }

        } catch (Exception e) {
            modelMap.put("msg", "fales");
            modelMap.put("errmsg", e.toString());
        }
        return JSON.toJSONString(modelMap);
    }




    /**
     * 动态更新某一个值或多个值
     *
     * @param json
     * @return
     */
    @RequestMapping("/updateByOne")
    public String updateByOne(@RequestBody JSONObject json, HttpServletRequest request) {
            String theirDb = mestUtil.getCompanyDB(request);
            System.out.println("==============================================");
            System.out.println("provider_file is " + json.toString() + "; provider_file is " + json);
            Map<String, Object> modelMap = new HashMap<String, Object>();
            apart_file record = json.toJavaObject(apart_file.class);
            return iafs.updateByOne(record,theirDb);
    }


}
